import util from '@/libs/util.js'
import {loadSystemMenus} from '@/api/setting/menu'

export default {
    namespaced: true,
    state: {
        menus: [],
        activeMenu: null,
        activeSubmenu: null,
        //扁平化处理
        authorizedMenus: [],
        loaded: false
    },
    mutations: {
        updateMenus(state, payload) {
            state.menus = payload.menus
        },
        updateSpread(state, payload) {
            state.authorizedMenus = payload.menus
        },
        setActive(state, payload) {
            state.activeMenu = payload.activeMenu
        },
        setActiveSub(state, payload) {
            state.activeSubmenu = payload.activeSubmenu
        }
    },
    actions: {
        load({state, dispatch}, update) {
            return new Promise(async resolve => {
                try {
                    if (util.checkLogin() && update) {
                        let menus = await loadSystemMenus();
                        await dispatch('stored', menus);
                    } else {
                        await dispatch('getStored')
                    }
                } catch (e) {

                }
                dispatch('spread');
                resolve()
            });
        },

        spread({state, commit, dispatch}) {
            console.log('0-0===')
            if (state.menus && state.menus.length > 0) {
                let array = [];
                let menus = [].concat(state.menus);
                util.spread(menus, array);
                commit('updateSpread', {menus: array});
                state.loaded = true
            } else {
                state.loaded = false
            }
        },

        stored({state, dispatch}, menus) {
            return new Promise(async resolve => {
                // store 赋值
                state.menus = menus;
                // 持久化
                await dispatch('admin/db/set', {
                    dbName: 'sys',
                    path: 'menus',
                    value: menus,
                    user: true
                }, {root: true});
                // end
                resolve()
            })
        },

        getStored({state, dispatch}) {
            return new Promise(async resolve => {
                // store 赋值
                // 持久化
                let menus = await dispatch('admin/db/get', {
                    dbName: 'sys',
                    path: 'menus',
                    user: true,
                    defaultValue: []
                }, {root: true});
                // end
                state.menus = menus;
                resolve()
            })
        },

        mapMenu(context, route) {
            let active = null;
            console.log(route)
            let name = route.name
            let menus = context.state.menus;
            menus.forEach(menu => {
                if (name.indexOf(menu.name) === 0) {
                    active = menu
                    context.commit("setActive", {activeMenu: active})
                }
            });

            if (active && active.children && active.children.length > 0) {
                let submenu = active.children
                submenu.forEach(menu => {
                    if (name.indexOf(menu.name) === 0) {
                        context.commit("setActiveSub", {activeSubmenu: menu})
                    }
                });
            } else {
                context.commit("setActiveSub", {activeSubmenu: null})
            }

        }
    },
    getters: {
        first() {

        }
    }
}
